/*
 * Copyright (c) 2007, Nubo Technologies
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *
 *    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *    * Neither the name of the Nubo Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.nubotech.gwt.oss.browser.client.ui;

import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ImageBundle;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.nubotech.gwt.oss.client.auth.AuthenticationManager;

/**
 *
 * @author jonnakkerud
 */
public class BrowserView extends Composite implements View {
    
    public static final String DEFAULT_DELIMITER = "/";
    
    public interface Images extends ImageBundle {
        @Resource("com/nubotech/gwt/oss/browser/public/images/folder_up.gif")
        AbstractImagePrototype upDirectory();
        @Resource("com/nubotech/gwt/oss/browser/public/images/folder.png")
        AbstractImagePrototype folder();
    }
    
    //private State state;
    private VerticalPanel main;
    protected FileView fileView;
    protected PathBar pathBar;
    private ActionBar actionBar;
    
    public BrowserView(Images images) {
        //this.state = state;
        initWidget(main = new VerticalPanel());
        setWidth("350px");
        initGui(images);
    }

    private void initGui(Images images) {
        main.setSpacing(3);
        
        main.add(new HeaderPanel());
        
        // add actionbar
        actionBar = new ActionBar();
        main.add(actionBar);
        
        VerticalPanel vp = new VerticalPanel();
        vp.setWidth("100%");
        vp.setSpacing(3);
        vp.setStyleName("file-view-container");
        RoundedPanel rp = new RoundedPanel(vp);
        rp.setCornerColor("#E1E7F2");
        main.add(rp);        
        
        // add breadcrumb navigator
        pathBar = new PathBar(images);
        vp.add(pathBar);
        
        // add file view
        fileView = new FileView(pathBar);
        fileView.setWidth("100%");
        vp.add(fileView);
    }
        
    private boolean hasCredential() {
        return (AuthenticationManager.instance().getLogin() != null);
    }
    
    public void refresh() {
        actionBar.setDefault();
        fileView.reload();
    }
    
    class ActionBar extends Composite {
        private HorizontalPanel actions = new HorizontalPanel();
        
        private SimplePanel container = new SimplePanel();
        
        public ActionBar() {
            actions.addStyleName("action-bar");
            actions.setWidth("100%");
            RoundedPanel rp = new RoundedPanel(container);
            
            initWidget(rp);
            initGui();
        }
        
        public void setDefault() {
            container.setWidget(actions);
        }
        
        private void initGui() {
            actions.setSpacing(5);
            
            final Widget createBucketWidget = WidgetFactory.createFloatWidget("Create Bucket", new CreateBucketPanel(BrowserView.this), new ClickListener() {
                public void onClick(Widget sender) {
                    setDefault();
                }
            });
            createBucketWidget.addStyleName("action-bar");
                        
            // create bucket action
            ClickableWidget createBucketCw = WidgetFactory.createAnchor("Create Bucket");
            createBucketCw.addClickListener(new ClickListener() {
                public void onClick(final Widget sender) {
                    if (hasCredential()) {
                        container.setWidget(createBucketWidget);
                    }
                }
            });
            actions.add(createBucketCw);
            
            // delete action
            ClickableWidget deleteCw = WidgetFactory.createAnchor("Delete");
            deleteCw.addClickListener(new ClickListener() {
                public void onClick(Widget sender) {
                    fileView.confirmDeleteSelected();
                }
            });
            actions.add(deleteCw);
            
            // upload file action
            ClickableWidget fileCw = WidgetFactory.createAnchor("Upload File");
            fileCw.addClickListener(new ClickListener() {
                public void onClick(final Widget sender) {
                    if (hasCredential()) {
                        container.setWidget(createFileUploadWidget());
                    }
                }
            });
            actions.add(fileCw);
            
            // create URL
            ClickableWidget urlCw = WidgetFactory.createAnchor("Create URL");
            urlCw.addClickListener(new ClickListener() {
                public void onClick(final Widget sender) {
                    if (hasCredential()) {
                        container.setWidget(createUrlWidget());
                    }
                }
            });
            actions.add(urlCw);
            
            setDefault();
        }
        
        private Widget createFileUploadWidget() {
            // upload file widget
            Widget uploadFileWidget = WidgetFactory.createFloatWidget("Upload File", new UploadFileWidget(BrowserView.this), new ClickListener() {
                public void onClick(Widget sender) {
                    setDefault();
                }
            });
            uploadFileWidget.addStyleName("action-bar");
            return uploadFileWidget;
        }
        
        private Widget createUrlWidget() {
            Widget createUrlWidget = WidgetFactory.createFloatWidget("Create URL", new CreateUrlWidget(BrowserView.this), new ClickListener() {
                public void onClick(Widget sender) {
                    setDefault();
                }
            });
            createUrlWidget.addStyleName("action-bar");
            return createUrlWidget;
           
        }
        
    }

    public void onShow() {
        fileView.reload();
    }
        
}
